(function(undefined){
	var admin_obj = {};

	//后台管理的广告管理部分
	admin_obj.adv = {};

	//根据城市ID联动出商家信息
	admin_obj.city_company=function(){
		$("#city_city").change(function(){
			var cityId=$("#city_city").val();
			if(null!= cityId && ""!=cityId){
				$.getJSON("/handle.php",{h:"company/city_company",id:cityId},function(data){
					if(data.state){
						var options="";
						myJSON=data.msg;
						if(myJSON){
							options="<option value='0'>--选择商家--</option>";
							for(var i in myJSON){
								options+="<option value="+myJSON[i].uid+">"+myJSON[i].title+"</option>";
							}
							$("#company").html(options);
						}
					} else
						{
						options="<option value='0'>--选择商家--</option>";
						$("#company").html(options);
					}
				});

			}else{
				options="<option value='0'>--选择商家--</option>";
				$("#company").html(options);
			}
		});
	}

	//根据城市ID联动出商家信息
	admin_obj.pro_city_company=function(){
		$("#city_pro").change(function(){

			setTimeout(admin_obj.init_company,"500");


		});
	}

	admin_obj.init_company=function(city,cid){
		var cityId=$("#city_city").val();
		if(!cityId){
			cityId=city;
		}
		if(null!= cityId && ""!=cityId){
			$.getJSON("/handle.php",{h:"company/city_company",id:cityId},function(data){
				if(data.state){
					var options="";
					myJSON=data.msg;
					if(myJSON){
						options="<option value='0'>--选择商家--</option>";
						for(var i in myJSON){
							sel='';
							if(myJSON[i].uid==cid){
								sel="selected='selected'";
							}
							options+="<option value="+myJSON[i].uid+" "+sel+">"+myJSON[i].title+"</option>";
						}
						$("#company").html(options);
					}
				} else
					{
					options="<option value='0'>--选择商家--</option>";
					$("#company").html(options);
				}
			});

		}else{
			options="<option value='0'>--选择商家--</option>";
			$("#company").html(options);
		}
	}

	//根据城市ID联动居委会、物管及服务站信息
	admin_obj.city_depart=function(){
		$("#city_city").change(function(){
			admin_obj.init_depart();
		});
	}

	//根据城市ID联动居委会、物管及服务站信息
	admin_obj.pro_depart=function(){
		$("#city_pro").change(function(){
			$('#city_city').val('');
			admin_obj.init_depart();
		});
	}

	//初始化城市下的居委会、物管、服务站
	admin_obj.init_depart=function(sid1,sid2,ser){
		cityId=city_num?city_num:$("#city_city").val();
		if(null!= cityId && ""!=cityId){
			$.getJSON("/handle.php",{h:"admin_community/city_depart",id:cityId},function(data){
				if(data.state){
					var options="";
					sec1=data.sec1;
					sec2=data.sec2;
					service=data.service;

					//选择居委会
					if(sec1){
						options="<option value='0'>--选择居委会--</option>";
						for(var i in sec1){
							sel='';
							if(sec1[i].uid==sid1){
								sel="selected='selected'";
							}
							options+="<option value="+sec1[i].uid+" "+sel+">"+sec1[i].title+"</option>";
						}
						$("#sec1").html(options);
					} else
						{
						options="<option value='0'>--选择居委会--</option>";
						$("#sec1").html(options);
					}

					//选择物管
					if(sec2){
						options="<option value='0'>--选择物管--</option>";
						for(var i in sec2){
							sel='';
							if(sec2[i].eid==sid2){
								sel="selected='selected'";
							}
							options+="<option value="+sec2[i].eid+" "+sel+">"+sec2[i].title+"</option>";
						}
						$("#sec2").html(options);
					} else
						{
						options="<option value='0'>--选择物管--</option>";
						$("#sec2").html(options);
					}

					//选择服务站
					if(service){
						options="<option value='0'>--选择服务站--</option>";
						for(var i in service){
							sel='';
							if(service[i].id==ser){
								sel="selected='selected'";
							}
							options+="<option value="+service[i].id+" "+sel+">"+service[i].name+"</option>";
						}
						$("#service").html(options);
					} else
						{
						options="<option value='0'>--选择服务站--</option>";
						$("#service").html(options);
					}
				}
				else{
					options="<option value='0'>--选择居委会--</option>";
					$("#sec1").html(options);
					options="<option value='0'>--选择物管--</option>";
					$("#sec2").html(options);
					options="<option value='0'>--选择服务站--</option>";
					$("#service").html(options);
				}

			});

		}else{
			options="<option value='0'>--选择居委会--</option>";
			$("#sec1").html(options);
			options="<option value='0'>--选择物管--</option>";
			$("#sec2").html(options);
			options="<option value='0'>--选择服务站--</option>";
			$("#service").html(options);
		}

	}


	admin_obj.material_init = function(){
		$.getJSON('/handle.php', {"h":"admin_material/list"}, function(data){
			if(!data.state){
				return false;
			}
			result = data.data;
			validlist_obj = $("#validlist");
			str = "";
			for(var i in result){
				str += '<div class="list_one" id="'+result[i].id+'" price="'+result[i].price+'" last="'+result[i].last+'" cellname="'+result[i].cellname+'" title="'+result[i].title+'"><span class="sp_one">'+result[i].title+'('+result[i].price+'元/'+result[i].cellname+')</span></div>';
			}
			validlist_obj.html(str);
			validlist_obj.find(".list_one").bind({
				"click": function(){
					tmp_obj = $(this);
					materiallist_obj = $("#materiallist");
					if(admin_obj.material_config.vals[tmp_obj.attr("id")]){
						return false;
					}
					if(parseInt(tmp_obj.attr("last")) <= 0){
						//没有数量也可以领取
						//return false;
					}
					admin_obj.material_config.vals[tmp_obj.attr("id")] = {"n":0,"price":tmp_obj.attr("price")};
					obj = $('<div class="list_one"><span class="sp_one">'+tmp_obj.attr("title")+'('+tmp_obj.attr("price")+'元/'+tmp_obj.attr("cellname")+')</span><input class="input_one" autocomplete="off" type="text" style="width: 120px;" n="'+tmp_obj.attr("id")+'" last="'+tmp_obj.attr("last")+'" name="m['+tmp_obj.attr("id")+']" value="0"></i></div>');
					materiallist_obj.append(obj);

					$("#materiallist :input[n="+tmp_obj.attr("id")+"]").live("change", function(){
						val = $(this).val();
						val = parseInt(val);
						last = $(this).attr("last");
						if(val < 1 || isNaN(val)){
							$(this).val(0);
							return false;
						}
						if(val > last){
							//没有数量也可以领取
							//val = last;
							//$(this).addClass("Validform_error");
						}
						$(this).val(val);
						admin_obj.material_config.vals[$(this).attr("n")].n = val;
						admin_obj.material_calc();
					})
					tmp_obj.remove();
				}
			})
		})
	}
	admin_obj.material_config = {
		vals:{}
	}
	//计算
	admin_obj.material_calc = function(){
		var total = 0;
		for(var i in admin_obj.material_config.vals){
			total += admin_obj.material_config.vals[i].n * admin_obj.material_config.vals[i].price;
		}
		total = total.toFixed(2);

		$(":input[name=estimate]").val(total);
	}


	if(window.admin_obj == undefined || window.city_obj == '未定义'){
		window.admin_obj = admin_obj;
	}
})()